<template>
    <a-drawer :title="title" :width="500" @close="handleCancel" :visible="visible" :maskClosable="false"
              :bodyStyle="{ paddingBottom: '60px' }">
        <a-form-model ref="ruleForm" :model="form" :rules="rules"

        >


            <a-row :gutter="16">
                <a-col :span="24">
                    <a-form-model-item label="门店封面" prop="picture" ref="picture">
                        <a-upload
                                list-type="picture-card"
                                class="avatar-uploader"
                                :show-upload-list="false"
                                :before-upload="beforeUpload"
                                @change="handleChange"
                                :customRequest="customRequest"
                        >
                            <img v-if="form.picture" :src="form.picture" class="picture" alt="请上传图片"/>
                            <div v-else>
                                <a-icon :type="loading ? 'loading' : 'plus'"/>
                                <div class="ant-upload-text">请上传一张门店封面图</div>
                            </div>
                        </a-upload>
                    </a-form-model-item>

                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="门店名称" prop="name" ref="name">
                        <a-input placeholder="请设置门店名称" v-model="form.name"/>
                    </a-form-model-item>
                </a-col>
                <a-col :span="12">
                    <a-form-model-item label="门店电话" prop="tel" ref="tel">
                        <a-input placeholder="请设置门店电话 " v-model="form.tel"/>
                    </a-form-model-item>
                </a-col>


                <a-col :span="24">
                    <a-form-model-item label="门店位置" prop="lng" ref="lng">
                        <a-row :gutter="8">
                            <a-col :span="10">
                                <a-input disabled placeholder="经度" v-model="form.lng"/>
                            </a-col>
                            <a-col :span="10">
                                <a-input disabled placeholder="纬度" v-model="form.lat"/>
                            </a-col>
                            <a-col :span="4">
                                <a-button type="primary" ghost @click="AmapSectorShow=true">
                                    <a-icon type="environment"/>
                                    标注
                                </a-button>
                            </a-col>
                        </a-row>
                    </a-form-model-item>
                </a-col>


                <a-col :span="24">
                    <a-form-model-item label="详细地址" prop="address" ref="address">
                        <a-input placeholder="详细地址" v-model="form.address"/>
                    </a-form-model-item>
                </a-col>
            </a-row>
            <AmapSector v-if="AmapSectorShow" @closeModal="closeAmapSector" :lnglat="{lng:form.lng,lat:form.lat}"/>

        </a-form-model>

        <!-- Modal 页脚 -->
        <footer class="drawer-footer">
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </footer>
    </a-drawer>
</template>
<script>
    import {shopProfileAdd, shopProfileUpdate} from '../public/http.js';
    import {fileUpload} from '../../../util/internal.api';

    export default {
        props: ['editRecord'],

        components: {
            AmapSector: () => import('../../../common/AmapSector'),
        },
        data() {
            return {
                isUpdate: false, //是否是更新模式
                loading: false, //按钮Loading
                title: '新增门店档案',
                visible: true, //modal 显示状态
                AmapSectorShow: false,

                form: {
                    picture: null,
                    name: null,
                    tel: null,
                    lat: null,
                    lng: null,
                    address: null,
                },
                rules: {
                    picture: {
                        required: true,
                        message: '请上传一张门店封面图',
                        trigger: 'blur'
                    },

                    name: [
                        {
                            required: true,
                            message: '门店名称不能为空',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '门店名称不能超过20个字',
                            trigger: 'blur'
                        }
                    ],


                    tel: [
                        {
                            required: true,
                            message: '门店电话不能为空',
                            trigger: 'blur'
                        },
                        {
                            max: 50,
                            message: '门店电话号码长度不超过50个字符',
                            trigger: 'blur'
                        }

                    ],

                    lng: {
                        required: true,
                        message: '请标注门店位置',
                        trigger: 'blur'
                    },

                    address: {
                        required: true,
                        message: '请填写门店详细地址',
                        trigger: 'blur'
                    },


                },

            };
        },
        methods: {

            //位置选择回调
            closeAmapSector(e) {

                this.AmapSectorShow = false;
                if (e.location) {
                    let {lng, lat, address} = e.location;
                    let ob = {lng, lat, address};
                    //合并对象
                    this.form = Object.assign(this.form, ob)
                }
                //关闭地图后自动验证地址
                this.$refs.lng.onFieldBlur();
                this.$refs.address.onFieldBlur();

            },


            //表单提交事件
            onSubmit() {
                this.$refs.ruleForm.validate(valid => {
                    //验证成功后进行数据提交
                    if (valid) {
                        this.loading = true;
                        if (this.isUpdate) {
                            shopProfileUpdate(this.form.id, this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true);
                                }
                            });

                        } else {
                            shopProfileAdd(this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true);
                                }
                            });
                        }
                    }
                });
            },


            //关闭弹窗
            handleCancel(render = false) {
                this.visible = false;
                this.$emit('closeFun', {render: render});
            },
            //表单初始化
            fromVal() {
                let obj = {...this.editRecord};
                if (obj.id) {
                    this.form = obj;
                    this.isUpdate = true;
                    this.title = '编辑档案信息';
                }
            },
            //数据初始化
            async init() {

                this.fromVal();

            },


            //门店封面上传方法
            customRequest(e) {
                const formData = new FormData();
                formData.append('file', e.file)
                fileUpload(formData).then((res) => {
                    this.loading = false;
                    if (res.code === 0) {
                        this.form.picture = res.data.url;
                    }
                })
            },
            //监听上传状态  uploading 开始
            handleChange(info) {

                if (info.file.status === 'uploading') {
                    this.loading = true;
                }
            },
            //上传之前数据检查
            beforeUpload(file) {
                const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
                if (!isJpgOrPng) {
                    this.$message.error('只允许上传JPG、PNG格式的图片文件');
                }
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('单个文件图片不能超过 2MB!');
                }
                return isJpgOrPng && isLt2M;
            },

        },

        created() {
            this.init();
        }
    };
</script>

<style scoped>

    .picture {
        width: 200px;
        height: 100px;
        overflow: hidden;
    }

</style>
